// SmarTest Studio - DeviceTab类

import { showNotification } from './utils.js';
import http from './http-client.js';

/**
 * DeviceTab类 - 负责管理设备标签页的功能
 */
export class DeviceTab {
    constructor() {
        // 绑定方法到实例
        this.loadDevices = this.loadDevices.bind(this);
    }
    
    /**
     * 加载设备列表
     */
    loadDevices() {
        http.get('/api/devices?api=load_devices')
            .then((response) => {
                const devices = response.data;
                const tableBody = document.getElementById('devicesTable');
                tableBody.innerHTML = '';

                devices.forEach((device) => {
                    const row = document.createElement('tr');
                    row.innerHTML = `
                        <td>${device.name}</td>
                        <td>
                            <a href="/device/${device.sn}" class="device-link">${device.sn}</a>
                        </td>
                        <td>${device.board}</td>
                        <td>${device.product}</td>
                        <td>${device.platform}</td>
                        <td>${device.version}</td>
                        <td>${device.domain}</td>
                        <td>${device.server}</td>
                        <td>${device.address}</td>
                        <td>
                            <span class="status-badge ${device.status === 'online' ? 'status-online' : 'status-offline'}">${device.status}</span>
                        </td>
                        <td>
                            <div class="btn-group">
                                <button class="btn btn-sm btn-outline-primary" title="命名">
                                    <i class="bi bi-pencil-square"></i>
                                </button>
                                <button class="btn btn-sm btn-outline-primary" title="打开">
                                    <i class="bi bi-box-arrow-up-right"></i>
                                </button>
                                <button class="btn btn-sm btn-outline-success" title="安装">
                                    <i class="bi bi-download"></i>
                                </button>
                                <button class="btn btn-sm btn-outline-warning" title="烧录">
                                    <i class="bi bi-device-hdd"></i>
                                </button>
                            </div>
                        </td>
                    `;
                    tableBody.appendChild(row);
                });
            })
            .catch((error) => {
                console.error('加载设备失败:', error);
                showNotification('加载设备失败', 'danger');
            });
    }
}